import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/widgets.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/utils/http.dart';
import 'package:www.znpigai.teacher/utils/progress_dialog.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<AvatarState> buildEffect() {
  return combineEffects(<Object, Effect<AvatarState>>{
    AvatarAction.onClick: _onClick,
  });
}

void _onClick(Action action, Context<AvatarState> ctx) async {
  if (GlobalState.isSignIn) {
    // 换头像
    var pr = new ProgressDialog(ctx.context)..setMessage('请等待...');
    pr.show();
    var url = '/correct/${GlobalState.userRole}/api/headimg/update.mvc';
    var result = await Http.post(url, data: {'path': action.payload});
    pr.hide();

    if (result.success) {
      Navigator.of(ctx.context).pop(action.payload);
    } else {
      Util.alert(result.errorMsg, ctx.context);
      Navigator.of(ctx.context).pop();
    }
  } else {
    Navigator.of(ctx.context).pop(action.payload);
  }
}
